#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @time     : 2020/12/11 15:36
# @Author   : BYF
# file      : dingding_test.py
# @version  :

import time
import hmac
import hashlib
import base64
import urllib.parse
import requests

timestamp = str(round(time.time() * 1000))
secret = 'SEC8999cd3226c00f3f572c8e306f0d0e736338025aa699dc1b97dc6b8430dddf46'
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
print(timestamp)
print(sign)

url = f"https://oapi.dingtalk.com/robot/send?access_token=2fda50f81cf79248959643147d9a5df624767a97f4ab69cd49711932dad4ac82&timestamp={timestamp}&sign={sign}"

headers = {
    'Content-Type': 'application/json'
}
data = {
    "msgtype": "text",
    "text": {
        "content": "我就是我, 是不一样的烟火"
    },
}

res = requests.post(url=url, json=data, headers=headers)
print(res.text)
